package com.example.tellwin.home.adpater;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tellwin.R;
import com.example.tellwin.mine.bean.AttentionQuestionBean;

import java.util.ArrayList;
import java.util.List;

public class AttentionQuestionAdapter extends RecyclerView.Adapter<AttentionQuestionAdapter.PopularKnowdegeViewHolder> {

    List<AttentionQuestionBean> attentionQuestionBeans = new ArrayList<>();
    Context context;

    AttentionQuestionListener attentionQuestionListener;

    public void setAttentionQuestionBeans(List<AttentionQuestionBean> attentionQuestionBeans) {
        this.attentionQuestionBeans = attentionQuestionBeans;
        notifyDataSetChanged();
    }

    public void setAttentionQuestionListener(AttentionQuestionListener attentionQuestionListener){
        this.attentionQuestionListener = attentionQuestionListener;
    }

    @NonNull
    @Override
    public PopularKnowdegeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new PopularKnowdegeViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_popular_knowdege, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull PopularKnowdegeViewHolder holder, int position) {
        AttentionQuestionBean bean = attentionQuestionBeans.get(position);
        Resources resources = context.getResources();
        if(position%3==0){
            holder.itemPopluarKnowdegeView.setBackgroundResource(R.drawable.bg_blue);
        }else if (position%3==1){
            holder.itemPopluarKnowdegeView.setBackgroundResource(R.drawable.bg_green);
        }else {
            holder.itemPopluarKnowdegeView.setBackgroundResource(R.drawable.bg_yellow);
        }
        holder.itemPopluarKnowdegeView.setPadding(resources.getDimensionPixelOffset(R.dimen.dp_15),
                resources.getDimensionPixelOffset(R.dimen.dp_0),
                resources.getDimensionPixelOffset(R.dimen.dp_0),
                resources.getDimensionPixelOffset(R.dimen.dp_16));
        holder.titleTv.setText(bean.getQuestionName());
        holder.contentTv.setText(bean.getAnswer());
        holder.likeIv.setSelected("1".equals(bean.getIsLike()));
        holder.likeIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(attentionQuestionListener!=null){
                    attentionQuestionListener.onItemLikeClick(position ,bean);
                }
            }
        });
        holder.itemPopluarKnowdegeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(attentionQuestionListener!=null){
                    attentionQuestionListener.onItemClick(position , bean);
                }
            }
        });
        holder.cancelAttentionTv.setVisibility(View.VISIBLE);
        holder.cancelAttentionTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(attentionQuestionListener!=null){
                    attentionQuestionListener.onAttentionItemClick(position , bean);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return attentionQuestionBeans.size();
    }

    class PopularKnowdegeViewHolder extends RecyclerView.ViewHolder {

         RelativeLayout itemPopluarKnowdegeView;
         TextView titleTv;
         TextView contentTv;
         ImageView shareIv;
         ImageView likeIv;
         TextView cancelAttentionTv;

        public PopularKnowdegeViewHolder(@NonNull View itemView) {
            super(itemView);
            itemPopluarKnowdegeView = (RelativeLayout) itemView.findViewById(R.id.item_popluar_knowdege_view);
            titleTv = (TextView) itemView.findViewById(R.id.title_tv);
            contentTv = (TextView) itemView.findViewById(R.id.content_tv);
            shareIv = (ImageView) itemView.findViewById(R.id.share_iv);
            likeIv = (ImageView) itemView.findViewById(R.id.like_iv);
            cancelAttentionTv = itemView.findViewById(R.id.cancel_attention_tv);
        }
    }

    public interface AttentionQuestionListener {
        void onItemLikeClick(int position, AttentionQuestionBean questionBean);
        void onItemShareClick(int position, AttentionQuestionBean questionBean);
        void onItemClick(int position, AttentionQuestionBean questionBean);
        void onAttentionItemClick(int position, AttentionQuestionBean questionBean);
    }
}
